﻿using System;
using System.Collections.Generic;
using System.Web;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Text;
using System.Security.Cryptography;
using System.Net;
using System.IO;
using System.Data.Common;
using MySql.Data.MySqlClient;

namespace WechatSDK
{
    public class DBS
    {
        string connectionString = @"Server=localhost;Database=cmu;Uid=root;Pwd=123654;Port=3306;";
        public string Get_DateTime()
        {



            DateTime Daytemp = DateTime.Now;
            string year = Daytemp.Year.ToString();
            string month = null;
            string day = null;
            month = Daytemp.Month.ToString();
            day = Daytemp.Day.ToString();
            if (Daytemp.Month.ToString().Length == 1)
                month = "0" + Daytemp.Month.ToString();

            if (Daytemp.Day.ToString().Length == 1)
                day = "0" + Daytemp.Day.ToString();


            return year + "/" + month + "/" + day + " " + DateTime.Now.ToLongTimeString();


        }
        public DataSet EQInfo_By_ID(string ID)
        {




            DataSet ds = new DataSet();
            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();


            string EQFlag = ID;
            string selectStr = "select 设备表.设备名称,设备表.设备型号,设备表.出厂编号,设备表.生产厂商,设备表.购入日期,Depts.Name,Depts.DeptNumber,设备表.Flag,设备表.存放位置,Dept2.Name FROM 设备表 JOIN Depts ON  设备表.所属科室= Depts.DeptNumber JOIN Depts Dept2 ON  Depts.PID= Dept2.DeptNumber where Flag= '" + EQFlag + "'"; ;



            MySqlDataAdapter myadp = new MySqlDataAdapter(selectStr, con); //适配器   
            myadp.Fill(ds); //将查询到数据填充到数据集  
            con.Close();

            return ds;

        }
        public string Add_Task_Log_File(string Flag, string FileName, string FileType, string ACCESS_TOKEN)
        {

            string fm = GetMultimedia(ACCESS_TOKEN, FileName);
            fm = fm.Replace("D:\\CMUImage\\", "");


            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string selectStr = "insert into 报修任务记录文件表 (Flag,文件名,文件类型) values('" + Flag + "','" + fm + "','" + FileType + "')";
            MySqlCommand mysqlcom = new MySqlCommand(selectStr, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();
            return "1";

        }





        public string Add_Task_Log(string LogFlag, string TaskFlag, string Content, string UserName)
        {



            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string selectStr = "insert into 报修任务记录表(Flag,Task_Flag,记录内容,记录人,记录时间) values('" + LogFlag + "','" + TaskFlag + "','" + Content + "','" + UserName + "','" + Get_DateTime() + "')";
            MySqlCommand mysqlcom = new MySqlCommand(selectStr, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();
            return "1";

        }


        public string Add_Task_File(string Flag, string FileName, string FileType, string ACCESS_TOKEN)
        {

            string fm = GetMultimedia(ACCESS_TOKEN, FileName);
            fm = fm.Replace("D:\\CMUImage\\", "");

            string t = DateTime.Now.ToShortDateString();





            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string selectStr = "insert into 报修任务文件表 (Flag,文件名,文件类型) values('" + Flag + "','" + fm + "','" + FileType + "')";
            MySqlCommand mysqlcom = new MySqlCommand(selectStr, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();
            return "1";






        }

        public string Add_Task(string Flag, string EQFlag, string Describe, string FaultCode, string CallPerson, string CallTime, string AnswerTime, string TaskPersonID, string Progress, string PictureCount, string VoiceCount, string VideoCount, string Department, string Hospital, string Type, string Repair_Cost, string CallPhone, string userid)
        {




            MySqlConnection con = new MySqlConnection(connectionString);
            con.Open();

            string selectStr = "insert into 报修任务表 (Flag,设备Flag,故障描述,故障代码,报修人,报修时间,回应时间,执行人,维修进程,图片数量,音频数量,视频数量,所属科室,所属医院,任务类别,维修费,生成日期,报修人电话,报修人微信UserID) values('" + Flag + "','" + EQFlag + "','" + Describe + "','" + FaultCode + "','" + CallPerson + "','" + CallTime + "','" + AnswerTime + "','" + TaskPersonID + "','" + Progress + "','" + PictureCount + "','" + VoiceCount + "','" + VideoCount + "','" + Department + "','" + Hospital + "','" + Type + "','" + Repair_Cost + "','" + Get_DateTime() + "','" + CallPhone + "','" + userid + "')";
            MySqlCommand mysqlcom = new MySqlCommand(selectStr, con);
            mysqlcom.ExecuteNonQuery();
            mysqlcom.Dispose();
            return "1";





        }



        private void MakeThumbnail(string sourcePath, string newPath, int width, int height)
        {
            System.Drawing.Image ig = System.Drawing.Image.FromFile(sourcePath);
            int towidth = width;
            int toheight = height;
            int x = 0;
            int y = 0;
            int ow = ig.Width;
            int oh = ig.Height;
            if ((double)ig.Width / (double)ig.Height > (double)towidth / (double)toheight)
            {
                oh = ig.Height;
                ow = ig.Height * towidth / toheight;
                y = 0;
                x = (ig.Width - ow) / 2;

            }
            else
            {
                ow = ig.Width;
                oh = ig.Width * height / towidth;
                x = 0;
                y = (ig.Height - oh) / 2;
            }
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(System.Drawing.Color.Transparent);
            g.DrawImage(ig, new System.Drawing.Rectangle(0, 0, towidth, toheight), new System.Drawing.Rectangle(x, y, ow, oh), System.Drawing.GraphicsUnit.Pixel);
            try
            {
                bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ig.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }

        }

        public string GetMultimedia(string ACCESS_TOKEN, string MEDIA_ID)
        {
            string file = string.Empty;
            string content = string.Empty;
            string strpath = string.Empty;
            string savepath = string.Empty;
            string stUrl = "https://qyapi.weixin.qq.com/cgi-bin/media/get?access_token=" + ACCESS_TOKEN + "&media_id=" + MEDIA_ID;
            // string stUrl = "http:///1.jpg";
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(stUrl);

            req.Method = "GET";
            string newfile;
            using (WebResponse wr = req.GetResponse())
            {
                HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();

                strpath = myResponse.ResponseUri.ToString();
                //WriteLog("接收类别://" + myResponse.ContentType);
                WebClient mywebclient = new WebClient();
                savepath = System.Web.HttpContext.Current.Server.MapPath("CMUImage") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next().ToString().Substring(0, 4) + ".jpg";
                // WriteLog("路径://" + savepath);
                try
                {
                    mywebclient.DownloadFile(strpath, savepath);
                    file = savepath;
                }
                catch (Exception ex)
                {
                    savepath = ex.ToString();
                }
                newfile = file.Replace("D:\\CMUImage\\", "");

            }
            MakeThumbnail(file, "D:\\CMUImage\\small_" + newfile, 100, 100);
            return file;
        }

    }
}